
export class Moment {
	// 日期
	date: Date | number | string
	constructor(date?: Date | number | string) {
		// 日期
		this.date = date || new Date()
	}
	// 日期格式化
	format(type: string = 'yyyy-MM-dd HH:mm:ss') {
		if (typeof this.date === 'number') this.date = new Date(this.date);
		if (typeof this.date === 'string') this.date = new Date(this.date);

		const d = this.date;
		const year = d.getFullYear();
		const mounth = d.getMonth() + 1;
		const date = d.getDate()
		const hour = d.getHours()
		const minute = d.getMinutes();
		const second = d.getSeconds();
		// 定义政策
		const reg = /(yyyy)|(MM)|(dd)|(HH)|(mm)|(ss)/g

		return type.replace(reg, function ($, $1) {
			switch ($) {
				case 'yyyy': return year.toString()
				case 'MM': return mounth.toString()
				case 'dd': return date.toString()
				case 'HH': return hour.toString()
				case 'mm': return minute.toString()
				case 'ss': return second.toString()
			}
			return $
		})
	}
}

// const m1 = new Moment();
// console.log(m1.format())
// const m2 = new Moment('2010-10-11 08:30:40');
// console.log(m2.format())
// const m3 = new Moment(1668498670540);
// console.log(m3.format())


type StorageType = typeof localStorage | typeof sessionStorage

export class Storage {
	storage: StorageType
	constructor(storage: StorageType) {
		this.storage = storage
	}
	setItem(key: string, value: any) {
		if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'string') {
			this.storage.setItem(key, value.toString())
		}

		if (typeof value === 'function' || typeof value === 'object') {
			this.storage.setItem(key, JSON.stringify(value))
		}

	}
	getItem(key: string) {
		const result = this.storage.getItem(key)
		if (result)
			return JSON.parse(result)
		return result
	}
	removeItem(key: string) {
		this.storage.removeItem(key)
	}
	clear() {
		this.storage.clear()
	}
}


export const l = new Storage(localStorage)
export const s = new Storage(sessionStorage)
// => 2022-11-15 15:29:30



export function moment(type: string = 'yyyy-MM-dd HH:mm:ss') {
	const d = new Date();

	const year = d.getFullYear()
	const month = d.getMonth() + 1
	const date = d.getDate()
	const hour = d.getHours()
	const minute = d.getMinutes()
	const second = d.getSeconds()

	return type.replace(/(yyyy)|(MM)|(dd)|(HH)|(mm)|(ss)/g, function ($) {
		switch ($) {
			case 'yyyy': return year.toString();
			case 'MM': return month.toString();
			case 'dd': return date.toString();
			case 'HH': return hour.toString();
			case 'mm': return minute.toString();
			case 'ss': return second.toString();
		}

		return '1'
	})
}


console.log(moment())
console.log(moment('HH:mm:ss'))
